﻿
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace ColonisatorGame
{
    public class MapIntercationObject : InteractionObject
    {
        public MapIntercationObject(string texturePath)
            : base(texturePath)
        {

        }

        public Vector2 GererDeplacementMap(ref KeyboardState keyboardState, ref MouseState mouseState,
            GraphicsDeviceManager Graphics, GameHUD hud, int offSetCoef)
        {
            // Gestion du déplacement de la carte
            var offest = new Vector2();
            var keys = keyboardState.GetPressedKeys();
            bool deplacementClavier = false;

            if (keys.Count() > 0)
            {
                foreach (var item in keys)
                {
                    switch (item)
                    {
                        case Keys.Up:
                            {
                                offest += DeplacerMapHaut(offest, offSetCoef, hud);
                                deplacementClavier = true;
                                break;
                            }
                        case Keys.Down:
                            {
                                offest += DeplacerMapBas(offest, offSetCoef, hud);
                                deplacementClavier = true;
                                break;
                            }
                        case Keys.Left:
                            {
                                offest += DeplacerMapGauche(offest, offSetCoef, hud);
                                deplacementClavier = true;
                                break;
                            }
                        case Keys.Right:
                            {
                                offest += DeplacerMapDroite(offest, offSetCoef, hud);
                                deplacementClavier = true;
                                break;
                            }
                        default:
                            break;
                    }
                }
            }

            if (!deplacementClavier)
            {
                if (mouseState.X >= Graphics.PreferredBackBufferWidth - 10)
                    offest = DeplacerMapDroite(offest, offSetCoef, hud);

                if (mouseState.X <= 10 && mouseState.X > 0)
                    offest = DeplacerMapGauche(offest, offSetCoef, hud);

                if (mouseState.Y >= Graphics.PreferredBackBufferHeight - 10)
                    offest = DeplacerMapBas(offest, offSetCoef, hud);

                if (mouseState.Y <= 10 && mouseState.Y > 0)
                    offest = DeplacerMapHaut(offest, offSetCoef, hud);
            }

            return offest;
        }

        private Vector2 DeplacerMapDroite(Vector2 offest, int offSetCoef, GameHUD hud)
        {
            if (hud.fenetrePrincipale.Offset.X < this.Texture.Width - hud.fenetrePrincipale.Cadre.Width)
                offest.X += offSetCoef;
            //    offest.X = 0;
            //else if (_hud.fenetrePrincipale.Offset.X > 0)
            return offest;
        }

        private Vector2 DeplacerMapGauche(Vector2 offest, int offSetCoef, GameHUD hud)
        {
            if (hud.fenetrePrincipale.Offset.X == 0)
                offest.X = 0;
            else if (hud.fenetrePrincipale.Offset.X > 0)
                offest.X -= offSetCoef;
            return offest;
        }

        private Vector2 DeplacerMapBas(Vector2 offest, int offSetCoef, GameHUD hud)
        {
            if (hud.fenetrePrincipale.Offset.Y < this.Texture.Height - hud.fenetrePrincipale.Cadre.Height)
                offest.Y += offSetCoef;
            //else if (_hud.fenetrePrincipale.Offset.Y > 0)
            //offest.Y += offSetCoef;
            return offest;
        }

        private Vector2 DeplacerMapHaut(Vector2 offest, int offSetCoef, GameHUD hud)
        {
            if (hud.fenetrePrincipale.Offset.Y == 0)
                offest.Y = 0;
            else if (hud.fenetrePrincipale.Offset.Y > 0)
                offest.Y -= offSetCoef;
            return offest;
        }
    }
}